package cn.lysj.framework.foodv2.controller;

import cn.lysj.framework.base.BaseController;
import cn.lysj.framework.common.CommonConstant;
import cn.lysj.framework.common.annotation.CurrentUser;
import cn.lysj.framework.common.domain.User;
import cn.lysj.framework.common.utils.pagination.PageVo;
import cn.lysj.framework.common.utils.web.Resp;
import cn.lysj.framework.common.validate.annotation.Validator;
import cn.lysj.framework.foodv2.domain.FoodType;
import cn.lysj.framework.foodv2.service.FoodTypeService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author drj
 * @description 菜品品类控制层
 * @create 2018-11-09 11:21:07
 **/
@Slf4j
@RestController
@Api(description = "菜品品类管理接口")
@RequestMapping("/food_type")
public class FoodTypeController extends BaseController<FoodType> {

    @Resource
    private FoodTypeService foodTypeService;

    @Override
    public FoodTypeService getService() {
        return foodTypeService;
    }

    @Override
    public Resp save(FoodType foodType) {
        return null;
    }

    @Override
    public Resp list(FoodType foodType, PageVo pageVo) {
        return null;
    }

    @PostMapping("/save_rewrite")
    public Resp save(@Validator FoodType foodType, @CurrentUser User user) {
        if (foodTypeService.getRepository().findByStoreIdAndNameAndDelFlag(user.getStoreId(), foodType.getName(), CommonConstant.NORMAL_FLAG) != null) {
            return new Resp(Resp.PARAM_ERROR, "添加菜品失败,该菜品已经存在!");
        }
        foodType.setStoreId(user.getStoreId());
        foodType.setUpdateTime(new Date());
        foodTypeService.save(foodType);
        return new Resp("添加成功!");
    }

    @GetMapping("/list_rewrite")
    public Resp list(@CurrentUser User user, FoodType foodType, PageVo pageVo) {
        pageVo.setPageSort("rank");
        pageVo.setPageOrder("desc");
        foodType.setStoreId(user.getStoreId());
        return new Resp(foodTypeService.list(foodType, pageVo));
    }

    @GetMapping("/list_selects")
    public Resp list(@CurrentUser User user) {
        Map<String, Object> map = new HashMap<>();
        map.put("content", foodTypeService.getRepository().findByStoreIdOrderByRankDesc(user.getStoreId()));
        return new Resp(map);
    }

    @GetMapping("select_item_rewrite")
    public Resp selectItem(@CurrentUser User user) {
        return new Resp(foodTypeService.getRepository().findFoodType(user.getStoreId()));
    }


}
